//
//  HistoricalDeliveryOrderViewController.m
//  FactorySale
//
//  Created by 李 on 2020/1/1.
//  Copyright © 2020 主事丫环. All rights reserved.
//  历史发货订单

#import "HistoricalDeliveryOrderViewController.h"
#import "HistoryFaHuoListCell.h"
#import "UnpaidSendOrderListModel.h"
#import "HistoryOrderDetailController.h"
@interface HistoricalDeliveryOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
//列表
@property (nonatomic,strong) UITableView *listTableView;
@property (nonatomic,strong) NSMutableArray *listArray;
@end
static NSString *const HistoryFaHuoListCellID = @"HistoryFaHuoListCell";

@implementation HistoricalDeliveryOrderViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self requestUnSendPartsOrders];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = Main_BackGroundColor;
    [self configViews];
    
}
-(void)configViews
{
    [self.listTableView registerNib:[UINib nibWithNibName:NSStringFromClass([HistoryFaHuoListCell class]) bundle:nil] forCellReuseIdentifier:HistoryFaHuoListCellID];
    [self.view addSubview:self.listTableView];
}
-(void)requestUnSendPartsOrders
{
    NSDictionary *param = @{
        @"id":NOTNIL(self.id),//种类id
        @"isPay":@"1"//（传0是未付款订单列表 传1 是历史发货单列表）
               
       };
    [self.listArray removeAllObjects];
    [NetWorkTool POST:unpaidSendOrderList param:param success:^(id dic) {
       
        
        self.listArray = [UnpaidSendOrderListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        [self.listTableView reloadData];
        
    } other:^(id dic) {
          [self.listTableView reloadData];
        
    } fail:^(NSError *error) {
          [self.listTableView reloadData];
    } needUser:NO];
}
- (UITableView *)listTableView
{
    if (!_listTableView) {
       _listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, KHEIGHT-kNaviHeight-45-10-kTabbarHeight) style:UITableViewStylePlain];
        _listTableView.hidden = NO;
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _listTableView.estimatedSectionHeaderHeight = 0;
        _listTableView.estimatedSectionFooterHeight = 0;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.backgroundColor = Main_BackGroundColor;
        _listTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:CGRectMake(0, 10, ScreenW, SCREEN_HEIGHT-kNaviHeight-45-10-10-kTabbarHeight) image:imgname(@"订单空界面") viewClick:^{
            
        }];
        UILabel * contentLabel;
        if (contentLabel) {
            [contentLabel removeFromSuperview];
        }
        contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-kTabbarHeight-200, ScreenW, 20)];
        contentLabel.text = @"暂无订单信息";
        contentLabel.textColor = [UIColor colorWithHexString:@"#B78030"];
        contentLabel.textAlignment = YES;
        contentLabel.font = [UIFont boldSystemFontOfSize:13];
        [_listTableView.placeHolderView addSubview:contentLabel];
    }
    return _listTableView;
}
#pragma mark UITableView
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   
    return 10;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 0.01;
    
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headerView.backgroundColor = Main_BackGroundColor;
    return headerView;
    
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
       headerView.backgroundColor = Main_BackGroundColor;
       return headerView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        HistoryFaHuoListCell *cell = [tableView dequeueReusableCellWithIdentifier:HistoryFaHuoListCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UnpaidSendOrderListModel *model  = [self.listArray safeObjectAtIndex:indexPath.section];
    cell.model = model;
    [cell.chaKanBtn addTarget:self action:@selector(chaKanBtnClick:) forControlEvents:UIControlEventTouchUpInside];
      
        return cell;
  
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 151;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UnpaidSendOrderListModel *model  = [self.listArray safeObjectAtIndex:indexPath.section];
    HistoryOrderDetailController *vc = [[HistoryOrderDetailController alloc]init];
    vc.id = model.id;
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(void)chaKanBtnClick:(UIButton *)button
{
    HistoryFaHuoListCell *cell = (HistoryFaHuoListCell *)[[button superview] superview];
    NSIndexPath *indexPath = [_listTableView indexPathForCell:cell];
    
    UnpaidSendOrderListModel *model  = [self.listArray safeObjectAtIndex:indexPath.section];
    HistoryOrderDetailController *vc = [[HistoryOrderDetailController alloc]init];
    vc.id = model.id;
    [self.navigationController pushViewController:vc animated:YES];
    
}

@end
